<?php


namespace app\api\controller;

use app\api\model\Member;
use app\api\model\Memberlog;
use think\Exception;
use yansongda\pay\Pay;

//https://cosimo.qswh.net.cn/api/Payresult/member

class Payresult extends BaseApi {

    public function payResult(){
        $pay = Pay::wechat($this->processConfig('payResult'));
        try{
            //返回的是数据集
            $data = $pay->verify();
            //查询出的订单金额
            $money = '';
            if($this->checkPrice($data,$money)){
                //TODO 按照业务逻辑修改订单

                //如有需要,发送模板消息
                $info = ['openid'=>$data->openid,'form_id'=>'prepay_id值'];
                $templateId = Config('template.template_order');
                $this->sendTemplateMessage($info,$templateId,$this->preData($data));
            }

        } catch (Exception $e) {
            $e->getMessage();
            error_log('异常错误:'.$e->getMessage(),3,dirname(__FILE__).'/notify.log');
        }

        return $pay->success()->send();
    }


    /* 购买会员的支付回调*/
    public function member(){
        $pay = Pay::wechat($this->processConfig('member'));
        try{
            //返回的是数据集
            $data = $pay->verify();
            $memberLog = new Memberlog();
            $logItem = $memberLog->where('logsn',$data->out_trade_no)->find();
            if(empty($logItem)){
                error_log('订单不存在或已删除,微信返回的订单编号为：'.$data->out_trade_no,3,dirname(__FILE__).'/member_notify.log');
            }else{
                //资金校验
                if($this->checkPrice($data,$logItem->price)){
                    //修改订单状态
                    $logItem->status = 1;
                    $logItem->save();
                    $member = new Member();
                    $memberItem = $member->getMember($data->openid);
                    $memberItem->identity = 4;//4自费会员
                    $memberItem->save();
                }
            }
        } catch (Exception $e) {
            $e->getMessage();
            error_log('异常错误:'.$e->getMessage(),3,dirname(__FILE__).'/member_notify.log');
        }

        return $pay->success()->send();
    }



    /**
     * 金额验证
     * @param $data            回调传入的数据
     * @param $money           订单金额
     * @return bool
     */
    protected function checkPrice($data,$money){
        if($data->return_code == 'SUCCESS' && $data->result_code == 'SUCCESS'){
            if($data->total_fee == $money * 100){
                return true;
            }else{
                error_log('支付金额不匹配,微信返回的金额为:'.$data->total_fee,3,dirname(__FILE__).'/notify.log');
            }
        }else{
            error_log('通信错误:'.print_r($data->all(),true),3,dirname(__FILE__).'/notify.log');
        }
    }

    /**
     * 处理模板内容
     *
     * @param $data     //订单中的内容
     * @return array    //返回处理好的数据
     */
    private function preData($data){
        $sendData = [
            'keyword1' => [
                'value' => '顺风速运',
            ],
            'keyword2' => [
                'value' => $data[0],
            ],
            'keyword3' => [
                'value' => $data[1]
            ],
            'keyword4' => [
                'value' => date('Y-m-d H:i:s',time())
            ]
        ];
        return $sendData;
    }










}